<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <div id="app">
    <h3>{{title}}</h3>
  </div>
  <script src="../vue3/reactive.js"></script>
  <script>
    const Vue = {
      createRenderer({ querySelector }) {
        return {
          createApp(options) {
            return {
              mount: function (selector) {
                if (options.setup) {
                  this.optionsSetup = options.setup()
                }
                if (options.data) {
                  this.optionsData = options.data()
                }

                this.proxy = new Proxy(this, {
                  get(target, key) {
                    console.log(target)
                    if (target.optionsSetup && key in target.optionsSetup) {
                      return target.optionsSetup[key]
                    }
                    if (target.optionsData && key in target.optionsData) {
                      return target.optionsData[key]
                    }
                  },
                  set(target, key, val) {
                    if (target.optionsSetup && key in target.optionsSetup) {
                      target.optionsSetup[key] = val
                    } else if (target.optionsData) {
                      target.optionsData[key] = val
                    }
                  }
                })
                // 建立更新机制
                this.update = effect(() => {
                  // 获取宿主元素
                  let parent = querySelector(selector)
                  // 获取到内容
                  let el = options.render.call(this.proxy)
                  // 将内容放在宿主元素上
                  parent.innerHTML = ''
                  parent.appendChild(el)
                })

              }
            }
          }
        }
      },
      createApp(options) {

        let renderer = Vue.createRenderer({
          querySelector(el) {
            return document.querySelector(el)
          }
        })

        return renderer.createApp(options)
      }
    }
  </script>
  <script>
    const app = Vue.createApp({
      setup() {
        let data = reactive({
          title: 'vue3, hello!'
        })

        setTimeout(() => {
          data.title = "hello, vue3!"
        }, 2000)

        return data
      },
      render() {
        const h3 = document.createElement('h3')
        h3.textContent = this.title
        return h3
      }
    })
    app.mount('#app')
  </script>
</body>

</html>